package cn.project.engineering.glide;

import android.content.Context;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;

/**
 * @author lifuhai
 * @date 2021/2/8 12:16
 */
@GlideModule
public class GlideModelConfig extends AppGlideModule {
    /**
     * 取1/8最大内存作为最大缓存
     */
    private static final long sMaxMemorySize =Runtime.getRuntime().maxMemory() / 8;
    private static final long DISk_SIZE = 100 * 1024 * 1024L;
    private static final String CACHE_FILE_NAME = "gwDownload";

    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        // 定义缓存大小
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, DISk_SIZE));
        // 定义存储卡缓存大小
        builder.setDiskCache(new ExternalPreferredCacheDiskCacheFactory(context, CACHE_FILE_NAME, DISk_SIZE));
        // 设置内存池大小
        builder.setMemoryCache(new LruResourceCache(sMaxMemorySize));
        // 设置图片池大小
        builder.setBitmapPool(new LruBitmapPool(sMaxMemorySize));
        // 图片格式使用 ARGB_565
        builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_RGB_565));
    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
    }
}
